/*
 * lj_proto_usart.c
 *
 *  Created on: 2017閿熸枻鎷�6閿熸枻鎷�27閿熸枻鎷�
 *      Author: 30655
 */
#include "stdio.h"
#include "xlink_ringbuf.h"
//#include "mico.h"
//#include "mkv.h"
//#include "linkkit_cli.h"
#include "lj_proto_usart.h"
//#include "caipu.h"

#include <hal/wifi.h>
#include <aos/aos.h>

//#define ZZC_LOG(fmt, ...)  custom_log("ZZC", fmt, ##__VA_ARGS__)
#define ZZC_LOG LOG

lj_uchar data_out[data_out_max] ;
lj_uchar data_in[data_in_max];
lj_uchar usart_get=0;
lj_uchar data_in_out[data_in_out_max];
lj_uchar lj_get_fd=0;
lj_uchar zzc_test=0;
lj_uchar init_pass=0;
lj_uchar hex_to_point_buf[data_in_out_max];

lj_uchar zijian_step;

#define NOTIFY_MAX (256)
#define NOTIFY_INDEX_MAX (32)
#define NOTIFY_CHAR_MAX (128)
#define NOTIFY_CHAR_NUM_MAX (2)

typedef struct{
	unsigned char count;
	unsigned char step;
	unsigned char buf[NOTIFY_MAX];
	unsigned char index_buf[NOTIFY_INDEX_MAX];
	unsigned char index_buf_num[NOTIFY_INDEX_MAX];
	unsigned char index_len;
	unsigned char had_change;
	unsigned char buf_char[NOTIFY_CHAR_NUM_MAX][NOTIFY_CHAR_MAX];
	unsigned char char_index_raw[NOTIFY_CHAR_NUM_MAX];
	unsigned char char_buf_len[NOTIFY_CHAR_NUM_MAX];
//	unsigned char char_buf_add_sum[NOTIFY_CHAR_NUM_MAX];

}notify_st_st;
notify_st_st notify_st;

unsigned char notify_buf_char_old[NOTIFY_CHAR_NUM_MAX][NOTIFY_CHAR_MAX];

#ifdef _with_spp_
#include "spp_setting.h"
extern unsigned char use_spp;
extern app_context_t *gb_Context;
#endif //_with_spp_


unsigned char all_wifi=0;
unsigned char all_net=0;
unsigned char all_error=0;
unsigned char get_query=0;
unsigned char notify_had_add=0;

unsigned char set_f8=0;
unsigned char set_f2=0;

unsigned char same_s_id=0;
#ifdef _use_caipu_
unsigned char lj_push_mode=LJ_PUSH_MODE_ORI;
unsigned char lj_setting[POINT_ALL_NUM_MAX+1]={0};
int lj_setting_len=0;
#endif //_use_caipu_
unsigned int lj_time_can_push=0;
unsigned char lj_force_push=0;
static unsigned char u_send_id=0xff;
static unsigned char u_rec_id=0xff;
unsigned char get_feedback=0;


unsigned int zhufan_time_min_total=0;




void ff_make_out(lj_usart_pkt *pkt);
unsigned short get_crc_modbus_head(unsigned char buf1,unsigned char buf2,unsigned char*buf ,unsigned int len );

extern void xlinkSetUartBaudrate(int baudrate);
int lj_baud[8]={9600,38400,57600,115200,230400,380400,921600,2400};

//extern unsigned char lj_setting;//20240227

unsigned char  check_change_86(unsigned char *buf,unsigned short len);
unsigned char set_86_setting(unsigned char *buf ,unsigned short len);
unsigned int time_can_up=0;
unsigned int time_can_up_set=0;
unsigned char check_change_86_all(unsigned char *buf,unsigned short len);
unsigned int time_can_up_dingshi=0;
unsigned int time_can_up_dingshi_set=10*30;//100ms
unsigned char  check_change_85(unsigned char *buf,unsigned short len);
unsigned char set_all_setting(unsigned char *buf,unsigned short len);
unsigned short setting_86_len=0;


lj_usart_pkt pkt_abuf;
lj_usart_pkt_st pkt_st_abuf;
lj_usart_pkt pkt_bbuf;
lj_usart_pkt pkt_con_buf;

unsigned int time_keep_update=0;
unsigned int bgl_change_times=0;

//unsigned char change_index_down(unsigned char buf);
//void change_dingshi_down(unsigned char *in,unsigned char *out);


unsigned char game_id[4];//game
unsigned char game_net[64];
unsigned char game_net_count;
void game_id_init(void);

void reset_old_imei(void);
void reset_old_sta(void);

lj_kind_st lj_kind;

void flash_write_kind(void);
void flash_read_kind(void);
void kind_init(void);

void copy_notify_buf_old(void);
unsigned char is_in_must_setting(unsigned char index);
unsigned char is_in_every_times_setting(unsigned char index);

//unsigned char cmd_buf[CMD_MAX]={0};


hex_cmd_st cmd_st;
static unsigned char sign=0;

void print_x(lj_uchar*data,lj_uint_16 len)
{
	lj_uint_16 i=0;
	for(i=0;i<len;i++)
	{
		u_printf("%02x ",data[i]);
	}
	u_printf("\r\n");
}

unsigned char  check_xor(lj_usart_pkt buf)
{
	lj_uchar sum=0;
	lj_uint_16 i=0;
	sum=buf.len_l;
	sum=sum^buf.len_h;
	sum=sum^buf.cmd;
//	if(buf.id_dir==d_dir_in)
//	{
		sum=sum^buf.r_id;
//	}
//	else
//	{
//		sum=sum^buf.s_id;
//	}
	for(i=0;i<buf.len_num-5;i++)
	{
		sum=sum^buf.data[i];
	}
	return sum;
}


unsigned char check_sum(lj_usart_pkt buf)
{
	unsigned char sum=0;
	unsigned short i=0;
	sum=sum+0x55+0xaa;
	sum=sum+buf.ver;
	sum+=buf.cmd;
	sum+=buf.len_h;
	sum+=buf.len_l;
	for(i=0;i<buf.len_num;i++)
	{
		sum+=buf.data[i];
	}
	return sum;
}





void lj_usart_pkt_init(lj_usart_pkt * pkt);




#ifdef zzc_zijian
void zijian_wifi(void)
{

}


void zijian_wifi_reset(void)
{
}

#endif

extern unsigned char kind_buf;

const unsigned char * CMD_BUF_P;



#ifdef _0319_
Kfifo  ring_u_data;
#define Q_U_LEN 1024
unsigned char Q_u_buf[Q_U_LEN]={0};
#endif


// #define _make_out_use_ring_
#ifdef _make_out_use_ring_

Kfifo make_out_ring;
#define M_RING_LEN 16
#define M_RING_MAX 256
unsigned char make_out_ring_buf[M_RING_LEN]={0};
unsigned char m_queue[M_RING_LEN][M_RING_MAX]={0};
unsigned char m_queue_buf[M_RING_MAX]={0};
unsigned char full[M_RING_LEN]={0};

void make_out_ring_init(void)
{
	Kfifo_Init(&make_out_ring, make_out_ring_buf, M_RING_LEN);
}

unsigned int m_ring_get(unsigned char *get_buf,unsigned int len)
{
	unsigned int ret=0;
	ret= Kfifo_Get(&make_out_ring,get_buf,len);
//	Kfifo_Add_Out(&make_out_ring,ret);
	return ret;
}

void m_ring_add_out(unsigned int len)
{
	Kfifo_Add_Out(&make_out_ring,len);
}

unsigned char m_ring_put(unsigned char* put_buf,unsigned int len)
{
	unsigned int ret=0;
	ret= Kfifo_Put(&make_out_ring,put_buf,len);
	return ret;
}

void m_ring_add_in(unsigned int len)
{
	Kfifo_Add_In(&make_out_ring,len);
}

void put_ring(unsigned char *buf,unsigned int len,unsigned char repeat_times)
{
	unsigned int i=0;
	unsigned char in_buf=0;
	if(len>=M_RING_MAX-3)
	{
		printf("buf  put int the ring is bigger than M_RING_MAX\r\n");
		len=M_RING_MAX-3;
	}
	printf("make_out_ring.in=%d\r\n",make_out_ring.in);
	in_buf=(make_out_ring.in&(M_RING_LEN-1));
	printf("in_buf=%d\r\n",in_buf);
	if(m_ring_put(&in_buf,1)==1)
	{
		m_queue[in_buf][0]=len/256;
		m_queue[in_buf][1]=len%256;
		m_queue[in_buf][2]=repeat_times;
		for(i=3;i<len+3;i++)
		{
			m_queue[in_buf][i]=buf[i-3];
		}
		m_ring_add_in(1);
	}
}

unsigned char send_ring(unsigned char set_freeback)
{
	static unsigned char out_buf=0;
	static unsigned char need_repeat_times=0;
	unsigned int out_count=0;
	unsigned int i=0;
	if(out_buf>=M_RING_LEN)
	{
		ZZC_LOG("send ring out_buf is not right");
	}
	else
	{
#ifdef _use_send_comfirm_
		if((need_repeat_times>0)&&(set_freeback==0))
		{
			need_repeat_times=need_repeat_times-1;
			out_count=m_queue_buf[0]*256+m_queue_buf[1];
			if((m_queue_buf[4]==0xfd)||(m_queue_buf[5]==0xfd))
			{
				u_send_id=m_queue_buf[8];
			}
			else
			{
				u_send_id=m_queue_buf[7];
			}
			pkt_st_abuf.usend(&m_queue_buf[3],out_count);
		/*
			out_count=m_queue[out_buf][0]*256+m_queue[out_buf][1];
			if((m_queue[out_buf][4]==0xfd)||(m_queue[out_buf][5]==0xfd))
			{
				u_send_id=m_queue[out_buf][8];
			}
			else
			{
				u_send_id=m_queue[out_buf][7];
			}
			pkt_st_abuf.usend(&m_queue[out_buf][3],out_count);
			*/
			ZZC_LOG("get into send ring repeat=%d",need_repeat_times);
			return 2;
		}
		else
		{
			need_repeat_times=0;
			if(m_ring_get(&out_buf,1)==1)
			{
				out_count=m_queue[out_buf][0]*256+m_queue[out_buf][1];
				for(i=0;i<out_count+3;i++)
				{
					m_queue_buf[i]=m_queue[out_buf][i];
				}
				m_ring_add_out(1);

				need_repeat_times=m_queue_buf[2];
				if((m_queue_buf[4]==0xfd)||(m_queue_buf[5]==0xfd))
				{
					u_send_id=m_queue_buf[8];
				}
				else
				{
					u_send_id=m_queue_buf[7];
				}
				pkt_st_abuf.usend(&m_queue_buf[3],out_count);


			//	m_queue[out_buf][0]=0xff;
				// pkt_st_abuf.usend(m_queue[out_buf],m_queue[out_buf][5]+8);

				/*
				need_repeat_times=m_queue[out_buf][2];
				if((m_queue[out_buf][4]==0xfd)||(m_queue[out_buf][5]==0xfd))
				{
					u_send_id=m_queue[out_buf][8];
				}
				else
				{
					u_send_id=m_queue[out_buf][7];
				}
				pkt_st_abuf.usend(&m_queue[out_buf][3],out_count);
				*/
				ZZC_LOG("get into send ring ");
				return 1;
			}
		}
#else
		if(m_ring_get(&out_buf,1)==1)
		{
			out_count=m_queue[out_buf][0]*256+m_queue[out_buf][1];
		//	m_queue[out_buf][0]=0xff;
			// pkt_st_abuf.usend(m_queue[out_buf],m_queue[out_buf][5]+8);
			pkt_st_abuf.usend(&m_queue[out_buf][3],out_count);
			return 1;
		}
#endif //_use_send_comfirm_
	}
	return 0;
}
#endif//_make_out_use_ring_

//#define _up_make_out_use_ring_
#ifdef _up_make_out_use_ring_

Kfifo up_ring;
#define UP_M_RING_LEN 4
#define UP_M_RING_MAX 512
unsigned char up_ring_buf[UP_M_RING_LEN]={0};
unsigned char up_m_queue[UP_M_RING_LEN][UP_M_RING_MAX]={0};
//unsigned char up_full[UP_M_RING_LEN]={0};

void up_ring_init(void)
{
	Kfifo_Init(&up_ring, up_ring_buf, UP_M_RING_LEN);
}

unsigned int up_m_ring_get(unsigned char *get_buf,unsigned int len)
{
	unsigned int ret=0;
	ret= Kfifo_Get(&up_ring,get_buf,len);
	// Kfifo_Add_Out(&up_ring,ret);
	return ret;
}

void up_ring_add_out(unsigned int len)
{
	Kfifo_Add_Out(&up_ring,len);
}



unsigned char up_m_ring_put(unsigned char* put_buf,unsigned int len)
{
	unsigned int ret=0;
	ret= Kfifo_Put(&up_ring,put_buf,len);
	return ret;
}

void up_ring_add_in(unsigned int len)
{
	Kfifo_Add_In(&up_ring,len);
}


void up_put_ring(unsigned char *buf,unsigned int len)
{
	unsigned int i=0;
	unsigned char in_buf=0;
	if(len+2>=UP_M_RING_MAX)
	{
		printf("buf  put int the ring is bigger than up_M_RING_MAX\r\n");
	}
	printf("up_ring.in=%d\r\n",up_ring.in);
	in_buf=(up_ring.in&(UP_M_RING_LEN-1));
	printf("in_buf=%d\r\n",in_buf);
	if(up_m_ring_put(&in_buf,1)==1)
	{
		up_m_queue[in_buf][0]=len/256;
		up_m_queue[in_buf][1]=len%256;
		for(i=2;i<len+2;i++)
		{
			up_m_queue[in_buf][i]=buf[i-2];
			printf("--%02x",buf[i-2]);
		}
		up_ring_add_in(1);
	}
}

extern int lj_user_post_raw_data(unsigned char *buf,unsigned short len);

unsigned char up_send_ring(void)
{
	unsigned char out_buf=0;
	unsigned short out_count=0;
	if(up_m_ring_get(&out_buf,1)==1)
	{
		out_count=up_m_queue[out_buf][0]*256+up_m_queue[out_buf][1];
	//	m_queue[out_buf][0]=0xff;
		// pkt_st_abuf.usend(m_queue[out_buf],m_queue[out_buf][5]+8);
		lj_user_post_raw_data(&up_m_queue[out_buf][2],out_count);
		up_ring_add_out(1);
		return 1;
	}
	return 0;
}
#endif//_up_make_out_use_ring_


//#define _up_tcp_ring_

#ifdef _up_tcp_ring_

Kfifo tcp_up_ring;
#define UP_TCP_RING_LEN 8
#define UP_TCP_RING_MAX 96
unsigned char tcp_up_ring_buf[UP_TCP_RING_LEN]={0};
unsigned char up_tcp_queue[UP_TCP_RING_LEN][UP_TCP_RING_MAX]={0};
//unsigned char up_full[UP_M_RING_LEN]={0};

void tcp_up_ring_init(void)
{
	Kfifo_Init(&tcp_up_ring, tcp_up_ring_buf, UP_TCP_RING_LEN);
}

unsigned int up_tcp_ring_get(unsigned char *get_buf,unsigned int len)
{
	unsigned int ret=0;
	ret= Kfifo_Get(&tcp_up_ring,get_buf,len);
	Kfifo_Add_Out(&tcp_up_ring,ret);
	return ret;
}

unsigned char up_tcp_ring_put(unsigned char* put_buf,unsigned int len)
{
	return Kfifo_Put(&tcp_up_ring,put_buf,len);
}

void tcp_up_put_ring(unsigned char *buf,unsigned int len)
{
	unsigned int i=0;
	unsigned char in_buf=0;
	if(len>=UP_TCP_RING_MAX)
	{
		printf("buf  put int the ring is bigger than up_tcp_RING_MAX\r\n");
	}
	printf("tcp_up_ring.in=%d\r\n",tcp_up_ring.in);
	in_buf=(tcp_up_ring.in&(UP_TCP_RING_LEN-1));
	printf("in_buf=%d\r\n",in_buf);
	if(up_tcp_ring_put(&in_buf,1)==1)
	{
		up_tcp_queue[in_buf][0]=len;
		for(i=1;i<len+1;i++)
		{
			up_tcp_queue[in_buf][i]=buf[i-1];
			printf("--%02x",buf[i-1]);
		}
	}
}

extern int lj_user_tcp_post_raw_data(unsigned char *buf,unsigned short len);

unsigned char tcp_up_send_ring(unsigned char *out,unsigned char *len)
{
	unsigned char out_buf=0;
	unsigned char out_count=0;
	if(up_tcp_ring_get(&out_buf,1)==1)
	{
		out_count=up_tcp_queue[out_buf][0];
	//	m_queue[out_buf][0]=0xff;
		// pkt_st_abuf.usend(m_queue[out_buf],m_queue[out_buf][5]+8);
//		lj_user_post_raw_data(&up_tcp_queue[out_buf][1],out_count);
		out=&up_tcp_queue[out_buf][1];
		*len=out_count;
		return 1;
	}
	return 0;
}
#endif//_up_tcp_ring_




void time_usart_fun(void)
{
	if(pkt_abuf.time_count<time_count_set)
	{
		pkt_abuf.time_count++;
	}
	else
	{
		pkt_abuf.time_out=1;
	}
}




#ifdef _zijian2_
void lj_usend_zijian(unsigned char res)
{

#if 0
	data_in_out[0]=res;
	//	pkt_st_abuf.set_dis(0);
//		lj_usart_pkt pkt_bbuf;
	pkt_bbuf.cmd=c_set_dis;
	pkt_bbuf.len_num=1;
//								data_in_out[0]=0x01;
	pkt_bbuf.id_dir=d_dir_in;
	pkt_bbuf.r_id=pkt_abuf.r_id;
#if defined  _chaoren_jhq_
	pkt_bbuf.cmd=0xb3;
	pkt_bbuf.mcu_ver=MCU_VER;//pkt_abuf.mcu_ver;
	pkt_bbuf.dec_kind=DEC_KIND;//pkt_abuf.dec_kind;
#endif
	pkt_bbuf.data=data_in_out;
//	pkt_bbuf.check_num=check_xor(data_in,2);
	ff_make_out(&pkt_bbuf);
#endif //0
	pkt_bbuf.ver=3;
	pkt_bbuf.cmd=ty_test_wifi;
	pkt_bbuf.len_num=0x01;
	pkt_bbuf.len_h=0x00;
	pkt_bbuf.len_l=0x01;
	pkt_bbuf.data=data_in_out;
	if(res==0)
	{
		data_in_out[0]=0;
	}
	else
	{
		data_in_out[0]=1;
	}
	make_out(&pkt_bbuf);

}
#endif

void lj_usend_zijian_ty(unsigned char res, signed char rssi)
{

	pkt_bbuf.ver=3;
	pkt_bbuf.cmd=ty_test_wifi;
	pkt_bbuf.len_num=0x02;
	pkt_bbuf.len_h=0x00;
	pkt_bbuf.len_l=0x02;
	pkt_bbuf.data=data_in_out;
	if(res==0)
	{
		data_in_out[0]=0;
	}
	else
	{
		data_in_out[0]=1;
	}
	if(rssi>-50)
	{
		data_in_out[1]=100;
	}
	else if(rssi<-100)
	{
		data_in_out[1]=0;
	}
	else
	{
		data_in_out[1]=100+((50+rssi)*100/50);
	}

	make_out(&pkt_bbuf);
}


#ifdef zzc_zijian

#define ZIJIAN_OFFSET 215



void flash_read_zijian(void)
{
//	hffile_userbin_read(ZIJIAN_OFFSET,(char *)(&zijian_step),1);
}

void flash_write_zijian(void)
{
//	hffile_userbin_write(ZIJIAN_OFFSET,(char *)(&zijian_step),1);
}

void zijian_init(void)
{
	zijian_step=0;
	flash_read_zijian();
}

unsigned int time_zijian;

void zijian_loop(void)
{
	if(zijian_step==1)
	{
		time_zijian++;
		if(time_zijian>1000)
		{
			time_zijian=0;
			zijian_wifi_reset();
			pkt_st_abuf.delayms(1000);
		//	pkt_st_abuf.delayms(100);
			pkt_st_abuf.reset_sdk();
			zijian_step=0;
			flash_write_zijian();
			pkt_st_abuf.delayms(100);
			pkt_st_abuf.restart();
		}
	}
}


#endif




void lj_usart_pkt_init(lj_usart_pkt * pkt)
{
	pkt->check_num=0;
	pkt->check_num1=0;
	pkt->cmd=0;
	pkt->data=NULL;
	pkt->len_h=0;
	pkt->len_l=0;
	pkt->len_num=0;
	pkt->r_id=0;
	pkt->s_id=0;


	pkt->id_dir=d_dir_in;
	pkt->time_out=0;
	pkt->step=0;
	pkt->ff_step=1;
	pkt->count=0;
	pkt->check_num_buf=0;
	pkt->time_count=0;
	lj_get_fd=0;
//	ZZC_LOG("lj_usart_pkt_init");
}

void lj_usend_wifi_net_status(lj_uchar wifi,lj_uchar net)//20231019
{
//	pkt_bbuf.cmd=0x01;//tuya
//
//	pkt_bbuf.id_dir=d_dir_out;
//	pkt_bbuf.len_num=2;
//	data_in_out[0]=all_wifi;
//	data_in_out[1]=all_net;
//	pkt_bbuf.data=data_in_out;
//	ff_make_out(&pkt_bbuf);
//	if(all_net==1)
//	{
//		time_can_up=10*5;
//		lj_force_push=1;
//	}

	pkt_bbuf.ver=3;
	pkt_bbuf.cmd=ty_net;
	pkt_bbuf.len_num=0x01;
	pkt_bbuf.len_h=0x00;
	pkt_bbuf.len_l=0x01;
	pkt_bbuf.data=data_in_out;
	if(wifi==0x03)
	{
		data_in_out[0]=0;
	}
	else if(net==1)
	{
		data_in_out[0]=4;
		if(time_can_up==0)
		{
			time_can_up=10*10;
		}
//		notify_init();
	}
	else if((wifi==0x02)||(wifi==0x00))
	{
		data_in_out[0]=2;
	}
	else if(wifi==1)
	{
		data_in_out[0]=3;
		return ;
	}
	else
	{
		data_in_out[0]=2;
	}
	make_out(&pkt_bbuf);


}


void lj_usend_wifi_net_status_ty_ask_net(lj_uchar wifi,lj_uchar net)//20231019
{
//	pkt_bbuf.cmd=0x01;//tuya
//
//	pkt_bbuf.id_dir=d_dir_out;
//	pkt_bbuf.len_num=2;
//	data_in_out[0]=all_wifi;
//	data_in_out[1]=all_net;
//	pkt_bbuf.data=data_in_out;
//	ff_make_out(&pkt_bbuf);
//	if(all_net==1)
//	{
//		time_can_up=10*5;
//		lj_force_push=1;
//	}

	pkt_bbuf.ver=3;
	pkt_bbuf.cmd=ty_ask_net;
	pkt_bbuf.len_num=0x01;
	pkt_bbuf.len_h=0x00;
	pkt_bbuf.len_l=0x01;
	pkt_bbuf.data=data_in_out;
	if(wifi==0x03)
	{
		data_in_out[0]=0;
	}
	else if(net==1)
	{
		data_in_out[0]=4;
//		if(time_can_up==0)
//		{
//			time_can_up=10*10;
//		}
//		notify_init();
	}
	else if((wifi==0x02)||(wifi==0x00))
	{
		data_in_out[0]=2;
	}
	else if(wifi==1)
	{
		data_in_out[0]=3;
		return ;
	}
	else
	{
		data_in_out[0]=2;
	}
	make_out(&pkt_bbuf);


}

void lj_usend_wifi_net_status_no_up(lj_uchar wifi,lj_uchar net)//20231019
{
	pkt_bbuf.cmd=0x01;

	pkt_bbuf.id_dir=d_dir_out;
	pkt_bbuf.len_num=2;
	data_in_out[0]=all_wifi;
	data_in_out[1]=all_net;
	pkt_bbuf.data=data_in_out;
	ff_make_out(&pkt_bbuf);
}



unsigned char raw_out_buf[CONTROL_BUF_MAX]={0};
void lj_usend_control_raw(unsigned char *buf,unsigned int len)
{
	unsigned int i=0,j=0,get_fd=0,add_count=0;
//	if(buf[3]==0x87)
//	{
//
//	}
//	else
//	{
		for(i=1;i<len-1;i++)
		{

			if(buf[i]==0xfd)
			{
				get_fd=1;
				continue;
			}
			if(get_fd==1)
			{
				get_fd=0;
				add_count++;
				if(j<CONTROL_BUF_MAX)
				{
				raw_out_buf[j]=buf[i]+0xf0;
				}
			}
			else
			{
				if(j<CONTROL_BUF_MAX)
				{
				raw_out_buf[j]=buf[i];
				}
			}
			j++;
		}
		ZZC_LOG("lj_usend_control_raw");
		for(i=0;i<len-7-add_count;i++)
		{
			printf("%02x",raw_out_buf[i]);
		}
		printf("\r\n");
		same_s_id=raw_out_buf[3];
		if(raw_out_buf[2]==0x87)
		{
//			set_86_setting(&raw_out_buf[4],j-5);
			set_all_setting(&raw_out_buf[4],j-5);
		}
		else
		{
		lj_usend_control(&raw_out_buf[4],j-5);
		}
//	pkt_st_abuf.usend(buf,len);
//	}
}


void lj_usart_pkt_st_init(lj_usart_pkt_st * pkt_st_buf)
{
	unsigned char j=0;
	pkt_st_abuf.ask_net=pkt_st_buf->ask_net;
	pkt_st_abuf.reset_sdk=pkt_st_buf->reset_sdk;
	pkt_st_abuf.restart=pkt_st_buf->restart;
	pkt_st_abuf.smart_link=pkt_st_buf->smart_link;
//	pkt_st_abuf.usart_send=pkt_st_buf->usart_send;
	pkt_st_abuf.get_mac=pkt_st_buf->get_mac;
	pkt_st_abuf.set_idkey=pkt_st_buf->set_idkey;
	pkt_st_abuf.get_idkey=pkt_st_buf->get_idkey;
	pkt_st_abuf.unfind=pkt_st_buf->unfind;
	pkt_st_abuf.unbind=pkt_st_buf->unbind;
	pkt_st_abuf.get_ver=pkt_st_buf->get_ver;
	pkt_st_abuf.get_time=pkt_st_buf->get_time;
	pkt_st_abuf.init_point_all=pkt_st_buf->init_point_all;
	pkt_st_abuf.publish_point_npush=pkt_st_buf->publish_point_npush;
	pkt_st_abuf.publish_point_npush_raw=pkt_st_buf->publish_point_npush_raw;
	pkt_st_abuf.publish_point_npush_raw_85=pkt_st_buf->publish_point_npush_raw_85;
	pkt_st_abuf.publish_point_push=pkt_st_buf->publish_point_push;
	pkt_st_abuf.publish=pkt_st_buf->publish;
	pkt_st_abuf.set_dis=pkt_st_buf->set_dis;
	pkt_st_abuf.usend=pkt_st_buf->usend;
	pkt_st_abuf.delayms=pkt_st_buf->delayms;
	pkt_st_abuf.get_id_work=pkt_st_buf->get_id_work;
	pkt_st_abuf.reload=pkt_st_buf->reload;
	pkt_st_abuf.get_ssid_pwd=pkt_st_buf->get_ssid_pwd;
	pkt_st_abuf.set_ssid_pwd=pkt_st_buf->set_ssid_pwd;

	pkt_st_abuf.test=pkt_st_buf->test;
//	pkt_st_abuf.set=pkt_st_buf->set;

	lj_usart_pkt_init(&pkt_abuf);
	notify_init();
//	lj_usart_pkt_init(&pkt_bbuf);
//	lj_usart_pkt_init(&pkt_con_buf);
#ifdef _0319_
	Kfifo_Init(&ring_u_data, Q_u_buf, Q_U_LEN);
	printf("zzc........kfifo_init\r\n");
#elif defined _make_out_use_ring_
	make_out_ring_init();
#endif
#ifdef _up_make_out_use_ring_
	up_ring_init();
#endif //_up_make_out_use_ring_
	init_pass=0xf5;
#ifdef _up_tcp_ring_
	tcp_up_ring_init();
#endif //_up_tcp_ring_

#ifdef _use_caipu_
	caipu_init(lj_usend_control);
#endif //_use_caipu_

}


void lj_usend_time_1(lj_time_t time_buf)
{

#if 1
	// lj_time_t time_buf;
	// pkt_st_abuf.get_time(&time_buf);
//		lj_usart_pkt pkt_bbuf;
	pkt_bbuf.cmd=ty_get_time;
	pkt_bbuf.len_num=8;//8
	pkt_bbuf.len_h=0;//8
	pkt_bbuf.len_l=8;//8
	data_in_out[0]=1;
	data_in_out[1]=(time_buf.year-2000)%256;
	data_in_out[2]=time_buf.month;
	data_in_out[3]=time_buf.day;
	data_in_out[4]=time_buf.hour;
	data_in_out[5]=time_buf.minute;
	data_in_out[6]=time_buf.second;
	data_in_out[7]=time_buf.week;
	pkt_bbuf.data=data_in_out;
//	pkt_bbuf.check_num=check_xor(data_in,2);
	make_out(&pkt_bbuf);

#endif

#if 0
	pkt_bbuf.cmd=0x08;
	pkt_bbuf.len_num=9;//8
	data_in_out[0]=time_buf.year/256;
	data_in_out[1]=time_buf.year%256;
	data_in_out[2]=time_buf.month;
	data_in_out[3]=time_buf.day;
	data_in_out[4]=time_buf.hour;
	data_in_out[5]=time_buf.minute;
	data_in_out[6]=time_buf.second;
	data_in_out[7]=time_buf.area;
	data_in_out[8]=time_buf.week;
	pkt_bbuf.data=data_in_out;
//	pkt_bbuf.check_num=check_xor(data_in,2);
	ff_make_out(&pkt_bbuf);

#endif// 0

}


#ifdef _CAIPU_USE_STRING_

unsigned char caipu_hex[CONTROL_BUF_MAX/2]={0};

//unsigned short Stringtohex(char *bcd,unsigned short len, unsigned char *hexArray) {
//    int i, j;
//    if(len==0)
//    {
//    	return 0;
//    }
//    if(bcd[len-1]==0)
//    {
//    	len=strlen(bcd);
//    }
//    // 纭繚BCD瀛楃涓茬殑闀垮害鏄伓鏁�
//    if (len % 2 != 0) {
//        printf("Invalid BCD string. It should have an even length.\n");
//        return 0;
//    }
//
//    // 灏嗘瘡2浣岯CD杞崲涓�1瀛楄妭16杩涘埗
//    for (i = 0, j = 0; i < len; i += 2, j++) {
//        // 鎻愬彇2浣岯CD骞惰浆鎹负16杩涘埗瀛楄妭
//    	if()
//        hexArray[j] = ((bcd[i] - '0') * 16 + (bcd[i + 1] - '0'));
//    }
//    return len/2;
//}

unsigned short string_to_hex_bytes(const char *str,  size_t len,unsigned char *hex_array) {
	if(len==0)
	    {
	    	return 0;
	    }
	    if(str[len-1]==0)
	    {
	    	len=strlen(str);
	    }
	    // 纭繚BCD瀛楃涓茬殑闀垮害鏄伓鏁�
	    if (len % 2 != 0) {
	        printf("Invalid BCD string. It should have an even length.\n");
	        return 0;
	    }

    for (size_t i = 0; i < len; i++) {
        // 姣忎袱涓瓧绗﹁〃绀轰竴涓瓧鑺�
        unsigned char high_nibble = str[i * 2] >= '0' && str[i * 2] <= '9' ? (str[i * 2] - '0') :
                                    (str[i * 2] >= 'A' && str[i * 2] <= 'F') ? (str[i * 2] - 'A' + 10) :
                                    (str[i * 2] >= 'a' && str[i * 2] <= 'f') ? (str[i * 2] - 'a' + 10) : 0;

        unsigned char low_nibble = str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9' ? (str[i * 2 + 1] - '0') :
                                   (str[i * 2 + 1] >= 'A' && str[i * 2 + 1] <= 'F') ? (str[i * 2 + 1] - 'A' + 10) :
                                   (str[i * 2 + 1] >= 'a' && str[i * 2 + 1] <= 'f') ? (str[i * 2 + 1] - 'a' + 10) : 0;

        // 缁勫悎楂樹綆鍥涗綅鍒颁竴涓瓧鑺�
        hex_array[i] = (high_nibble << 4) | low_nibble;
    }
    return len/2;
}


#endif //_CAIPU_USE_STRING_

#if 0

void lj_usend_control(unsigned char* data,unsigned short data_len)
{
	unsigned short i=0;
	unsigned char index=0;
	unsigned char kind=0;
	unsigned short len=0;
	unsigned short len_hex=0;
//	unsigned char index_count=0;
//	unsigned char m=0,n=0,k=0;
//	unsigned char id_buf[12][8]={0};
//	unsigned char buff[20]={0};
//	unsigned char is_wrong=0;
//	unsigned char data_buf[256+12]={0};
//	unsigned char dou_num=0;
	if(time_can_up_set==0)
	{
		time_can_up=TIME_CAN_UP_MAX;
	}
	else
	{
		time_can_up=time_can_up_set;
	}
	lj_force_push=1;
	for(i=0;i<data_len;)
	{
		index=data[i];
		kind=data[i+1]&0xf0;
		len=(data[i+1]&0x0f)*256+data[i+2];
		if(index==0x0e)
		{
			lj_force_push=1;
			if(data[i+3]==0)
			{
				time_can_up_set=10*60*5;
			}
			else
			{
				time_can_up_set=10*60*data[i+3];//100ms
			}
			time_can_up=time_can_up_set;
			time_can_up_dingshi=0;
		}
#ifdef _use_caipu_
		if(index==CAIPU_WIFI_SET_UPDATE)
		{
//			if(data[i+3]>1)
//			{
				pkt_st_abuf.set(&data[i+3],len);
				pkt_st_abuf.publish_point_npush_raw_85(&data[i],len+3);
//			}
			return ;
		}
#endif //_use_caipu_
#ifdef _use_caipu_
		if(index==CAIPU_WIFI_PIP)
		{
#ifdef _CAIPU_USE_STRING_
			len_hex=string_to_hex_bytes(&data[i+3],len,caipu_hex);
			ZZC_LOG("len_hex=%d",len_hex);
			if(len_hex!=0)
			{
				print_x(caipu_hex,len_hex);
				caipu_point_deal(caipu_hex,len_hex);
			}
#else
			caipu_point_deal(&data[i+3],len);
#endif
			return ;
		}
		if(index==CAIPU_WIFI_END)
		{
			need_end_caipu_fun();
			return;
		}
#endif //_use_caipu_
		i=i+len+3;
	}
/*
#ifdef _use_caipu_
	if(caipu_point_deal(data,data_len))
	{
		return ;
	}
//	ff_make_out(&pkt_bbuf);
#endif //_use_caipu_
*/


	pkt_bbuf.id_dir=d_dir_out;
	pkt_bbuf.cmd=c_send_point;
	pkt_bbuf.len_num=data_len;
	pkt_bbuf.data=data;
	pkt_bbuf.repeat_times=REPEAT_TIME_MAX;
	ff_make_out(&pkt_bbuf);

}


#endif //0


unsigned char ty_change_kind_up(unsigned char kind,unsigned char len)
{
	switch(kind)
	{
	case 0x00:
		return 0xa0;
	case 0x01:
		return 0xb0;
	case 0x02:
		return 0x30;
	case 0x03:
		return 0x90;
	case 0x04:
		return 0x00;
	case 0x05:
		if(len==1){return 0x00;}
		else if(len==2){return 0x20;}
		else if(len==4){return 0x40;}
		else{return 0xa0;}
	default:
		return 0x00;
	}
}


unsigned char ty_change_kind_down(unsigned char kind)
{
	switch(kind)
	{
	case 0xa0:
		return 0x00;
	case 0xb0:
		return 0x01;
	case 0x30:
		return 0x02;
	case 0x90:
		return 0x03;
	case 0x20:
		return 0x05;
	default:
		return 0x04;
	}
}

unsigned int time_query_send_net=0;

void lj_usend_control(unsigned char* data,unsigned short data_len)
{
	unsigned short i=0;
	unsigned char index=0;
	unsigned char kind=0;
	unsigned short len=0;
	unsigned short len_hex=0;
	unsigned char point_buf[128]={0};
	unsigned char point_count=0;
	unsigned short j=0;
	unsigned char index_buf=0;
	if(time_can_up_set==0)
	{
		time_can_up=TIME_CAN_UP_MAX;
	}
	else
	{
		time_can_up=time_can_up_set;
	}
	lj_force_push=1;
	for(i=0;i<data_len;)
	{
		index_buf=data[i];
		index=index_buf;
		if(index_buf==0xee)
		{
			index=0x0e;
		}
		kind=data[i+1]&0xf0;
		len=(data[i+1]&0x0f)*256+data[i+2];
		point_count=0;
		switch(kind)
		{
		case 0x00:
		case 0xb0:
			if(index_buf==0x0e)
			{
				lj_force_push=1;

				if((data[i+3]==0)||(data[i+3]==1))
				{
					time_can_up_set=10*60*5;
				}
				else
				{
					time_can_up_set=10*60*data[i+3];//100ms
				}
				time_can_up=time_can_up_set;
				time_can_up_dingshi=0;

//			point_buf[point_count]=index;point_count++;
//			point_buf[point_count]=ty_change_kind_down(kind);point_count++;
//			point_buf[point_count]=len;point_count++;
//			for(j=0;j<len;j++)
//			{
//				point_buf[point_count+j]=data[i+3+j];
//				point_count++;
//			}
//			pkt_bbuf.ver=3;
//			pkt_bbuf.cmd=ty_query;
//			pkt_bbuf.len_num=0;
//			pkt_bbuf.data=point_buf;
//			pkt_bbuf.repeat_times=0;
//			make_out(&pkt_bbuf);

			lj_usend_wifi_net_status(0x02,0x00);
			time_query_send_net=2;//100ms

			get_query=1;

//			pkt_st_abuf.delayms(500);
//			lj_usend_wifi_net_status(all_wifi,all_net);
			}
			else
			{
				point_buf[point_count]=index;point_count++;
				point_buf[point_count]=ty_change_kind_down(kind);point_count++;
				point_buf[point_count]=len/256;point_count++;
				point_buf[point_count]=len%256;point_count++;
				for(j=0;j<len;j++)
				{
					point_buf[point_count+j]=data[i+3+j];
//					point_count++;
				}
				point_count=point_count+len;
				pkt_bbuf.ver=3;
				pkt_bbuf.cmd=ty_control;
				pkt_bbuf.len_num=point_count;
				pkt_bbuf.data=point_buf;
				pkt_bbuf.repeat_times=0;
				make_out(&pkt_bbuf);
			}
			break;
		case 0x20:
		case 0x30:
		case 0x40:
		case 0x90:
			if(len+4>128)
			{
				ZZC_LOG("control buf is bigger than 128");
				return;
			}
			point_buf[point_count]=index;point_count++;
			point_buf[point_count]=ty_change_kind_down(kind);point_count++;
			point_buf[point_count]=len/256;point_count++;
			point_buf[point_count]=len%256;point_count++;
			for(j=0;j<len;j++)
			{
				point_buf[point_count+j]=data[i+3+j];

			}
			point_count=point_count+len;
			pkt_bbuf.ver=3;
			pkt_bbuf.cmd=ty_control;
			pkt_bbuf.len_num=point_count;
			pkt_bbuf.data=point_buf;
			pkt_bbuf.repeat_times=0;
			make_out(&pkt_bbuf);
			break;

		case 0xa0:
			if(len+4>128)
			{
				ZZC_LOG("control buf is bigger than 128");
				return;
			}
			point_buf[point_count]=index;point_count++;
			point_buf[point_count]=ty_change_kind_down(kind);point_count++;
			point_buf[point_count]=len/256;point_count++;
			point_buf[point_count]=len%256;point_count++;
			for(j=0;j<len;j++)
			{
				point_buf[point_count+j]=data[i+3+j];

			}
			point_count=point_count+len;
			pkt_bbuf.ver=3;
			pkt_bbuf.cmd=ty_control;
			pkt_bbuf.len_num=point_count;
			pkt_bbuf.data=point_buf;
			pkt_bbuf.repeat_times=0;
			make_out(&pkt_bbuf);
			break;

		}

		i=i+len+3;
	}
//	pkt_bbuf.cmd=c_send_point;
//	pkt_bbuf.len_num=data_len;
//	pkt_bbuf.data=data;
//	pkt_bbuf.repeat_times=REPEAT_TIME_MAX;
//	ff_make_out(&pkt_bbuf);

}




unsigned int get_status(unsigned char index)
{
	unsigned int i=0;
	data_in_out[i+0]=0x10+index*20;
	data_in_out[i+1]=0x00;
	data_in_out[i+2]=0x01;
	data_in_out[i+3]=0;
	i=i+4;
	return i;
}


const unsigned char TabH[]  = {  //CRC?????????
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
       0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
       0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
       0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
       0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
       0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
       0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
       0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
       0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
       0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
       0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
       0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
       0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
       0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
       0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
   };
const  unsigned char  TabL[] = {  //CRC?????????
       0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
       0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
       0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
       0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
       0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
       0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
       0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
       0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
       0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
       0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
       0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
       0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
       0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
       0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
       0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
       0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
       0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
       0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
       0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
       0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
       0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
       0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
       0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
       0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
       0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
       0x43, 0x83, 0x41, 0x81, 0x80, 0x40
   };


unsigned short get_crc_modbus(unsigned char*buf ,unsigned int len )
{
	unsigned short index;
	unsigned char crch = 0xFF;
	unsigned char crcl = 0xFF;
	while (len--)  //?$?????????RC
	{
		index = crch ^ *buf++;
		crch = crcl ^ TabH[ index];
		crcl = TabL[ index];
	}

	return (((crch<<8) | crcl)&(0x0000ffff));
}

unsigned short get_crc_modbus_head(unsigned char buf1,unsigned char buf2,unsigned char*buf ,unsigned int len )
{
	unsigned short index;
	unsigned char crch = buf1;
	unsigned char crcl = buf2;
	while (len--)  //?$?????????RC
	{
		index = crch ^ *buf++;
		crch = crcl ^ TabH[ index];
		crcl = TabL[ index];
	}

	return (((crch<<8) | crcl)&(0x0000ffff));
}

//modbus潞脥xmodem鹿芦脢陆虏禄脪禄脩霉拢卢modebus脢脟 8005  碌芦xmodem脢脟1021 虏茅驴麓http://www.ip33.com/crc.html

unsigned int get_crc(unsigned char*buf ,unsigned int len)
{
	int crc=0;
	int i=0,j=0;
	for(i=0;i<len;i++)
	{
		crc=crc^buf[i]<<8;
		for(j=0;j<8;j++)
		{
			if((crc&((int) 0x8000))!=0)
			{
				crc=crc<<1^0x1021;
			}
			else
			{
				crc=crc<<1;
			}
		}
	}
	return crc&0xffff;
}


unsigned int rec_timeout=0;

void time_get_deal_reset(void)
{
	rec_timeout=0;
}


#define NOTIFY_SEND_BUF_MAX (500)
unsigned char notify_send_buf[NOTIFY_SEND_BUF_MAX]={0};

void time_get_deal_loop(void)
{
	unsigned short i=0;
	unsigned short j=0;
	unsigned short count=0;
	rec_timeout++;
//	ZZC_LOG("rec_timeout=%d",rec_timeout);
	if(rec_timeout>1)
	{
//		rec_timeout=0;
		//like_rec_time=0;

//		if(notify_st.count)
//		{
		if(notify_had_add)
		{
			notify_had_add=0;
			ZZC_LOG("notify_had_add");
			if((time_can_up)||(notify_st.had_change&0x02))
			{
				if(notify_st.had_change)
				{
					ZZC_LOG("need_notify");
					if(notify_st.had_change&0x02)
					{
						ZZC_LOG("must notify");
					}
					if(all_net==1)
					{
						notify_st.had_change=0;
					}
					else
					{
						ZZC_LOG("all_net==0 and not reset notify_st.had_change ");
					}
	//				pkt_abuf.len_num=notify_st.count;
					for(i=0;i<notify_st.count;i++)
					{
						notify_send_buf[i]=notify_st.buf[i];
					}
					count=notify_st.count;
					for(j=0;j<NOTIFY_CHAR_NUM_MAX;j++)
					{
						if(notify_st.char_buf_len[j]>0)
						{
							if(count+notify_st.char_buf_len[j]>NOTIFY_SEND_BUF_MAX)
							{
								break;
							}
							for(i=0;i<notify_st.char_buf_len[j];i++)
							{
								notify_send_buf[count+i]=notify_st.buf_char[j][i];
							}
							count=count+i;
						}
					}
					pkt_st_abuf.publish_point_npush_raw_85(notify_send_buf,count);
	//				pkt_st_abuf.publish_point_npush_raw_85(notify_st.buf,notify_st.count);
					copy_notify_buf_old();
					ZZC_LOG("notify up count=%d",count);
				}
				else
				{
					ZZC_LOG("notify the same");
				}
			}
			else
			{
				ZZC_LOG("!!time_can_up==0");
			}
		}
//		}
//		notify_init();
//		lj_usart_pkt_init(&pkt_abuf);
	//	ZZC_LOG("like_rec_time>100\r\n");
	}
}


//char pid_buf[21]={0};
//char bin_buf[6]={0};
//unsigned char sta_index=0;
//unsigned char sta_count=0;
//unsigned int like_rec_time=0;
// unsigned char bgl_change=0;
// unsigned int bgl_change_times=0;//100ms




void ff_make_out(lj_usart_pkt *pkt)
{
	lj_uint_16 add_count=0;
//	lj_uint_16 new_count=0;
	lj_uint i=0;
	lj_uchar buf=0;
	lj_uchar check_num=0;
	// unsigned int  mk_len=0;
	ZZC_LOG("get in to ff_make_out ");
	if(pkt_st_abuf.usend==NULL)
	{
		return;
	}

	if(pkt->len_num>data_out_max-7)
	{
		return;
	}
	data_out[0]=0xff;
	buf=((pkt->len_num)+5)>>8;
	check_num=check_num^buf;
	if((buf==0xff)||(buf==0xfe)||(buf==0xfd))
	{
		data_out[1+add_count]=0xfd;
		add_count++;
		data_out[1+add_count]=buf&0x0f;
	}
	else
	{
		data_out[1+add_count]=buf;
	}
//	data_out[1+add_count]=((pkt->len_num)+5)>>8;
	buf=pkt->len_num+5;
	check_num=check_num^buf;
	if((buf==0xff)||(buf==0xfe)||(buf==0xfd))
	{
		data_out[2+add_count]=0xfd;
		add_count++;
		data_out[2+add_count]=buf&0x0f;
	}
	else
	{
		data_out[2+add_count]=buf;
	}
//	data_out[2]=pkt->len_num+5;
	buf=pkt->cmd;
	check_num=check_num^buf;
	if((buf==0xff)||(buf==0xfe)||(buf==0xfd))
	{
		data_out[3+add_count]=0xfd;
		add_count++;
		data_out[3+add_count]=buf&0x0f;
	}
	else
	{
		data_out[3+add_count]=buf;
	}
//	data_out[3]=pkt->cmd;
	if(pkt->id_dir==d_dir_out)
	{

		if(same_s_id==0xf1)
		{
			pkt->s_id=0;
		}
		else
		{
			if(pkt->s_id>=0xf0)
			{
				pkt->s_id=1;
			}
			else
			{
				pkt->s_id++;
			}
		}
		data_out[4+add_count]=pkt->s_id;
//		u_send_id=pkt->s_id;
		buf=pkt->s_id;
		check_num=check_num^buf;
	}
	else
	{
		pkt_bbuf.repeat_times=0;
	//	data_out[4]=pkt->r_id;
		buf=pkt->r_id;
		check_num=check_num^buf;
		if((buf==0xff)||(buf==0xfe)||(buf==0xfd))
		{
			data_out[4+add_count]=0xfd;
			add_count++;
			data_out[4+add_count]=buf&0x0f;
		}
		else
		{
			data_out[4+add_count]=buf;
		}
	}

	for( i=0;i<pkt->len_num;i++)
	{
		data_out[5+i+add_count]=pkt->data[i];
		check_num=pkt->data[i]^check_num;
		if((pkt->data[i]==0xff)||(pkt->data[i]==0xfe)||(pkt->data[i]==0xfd))
		{
		//	data_out[i]=0xfd;
			add_count++;
			if(add_count+pkt->len_num>data_out_max-7)
			{
				return;
			}
			data_out[5+i+add_count-1]=0xfd;
		//	i++;
			if(pkt->data[i]==0xff)
			{
				data_out[5+i+add_count]=0x0f;
			}
			else if(pkt->data[i]==0xfe)
			{
				data_out[5+i+add_count]=0x0e;
			}
			else if(pkt->data[i]==0xfd)
			{
				data_out[5+i+add_count]=0x0d;
			}
		}
	}
//	check_num=check_num^data_out[1]^data_out[2]^data_out[3]^data_out[4];
	if((check_num==0xff)||(check_num==0xfd)||(check_num==0xfe))//20170802
	{
		data_out[add_count+pkt->len_num+5]=0xfd;
		check_num=check_num&0x0f;
		add_count++;
	}
	data_out[add_count+pkt->len_num+5]=check_num;
	data_out[add_count+pkt->len_num+1+5]=0xfe;
//#ifdef zzc_test
	if(zzc_test==1)
	{
	print_x(data_out,(pkt->len_num)+7+add_count);
	}
	else
	{
//#else
#ifdef _0319_
//		memset(Q_u_buf,0,Q_U_LEN);
		Kfifo_Put(&ring_u_data, data_out, (pkt->len_num)+7+add_count);
		printf("zzc............kififo  put  len=%d\r\n",(pkt->len_num)+7+add_count);
//		Kfifo_Add_Out(&ring_buf_data,TempGLen);
#elif defined _make_out_use_ring_
//	data_out[0]=(pkt->len_num)+7+add_count;
	put_ring(data_out,(pkt->len_num)+7+add_count,pkt->repeat_times);
#else
	pkt_st_abuf.usend(data_out,(pkt->len_num)+7+add_count);
#endif
	}
}


void make_out(lj_usart_pkt *pkt)
{
	unsigned char i=0;
	data_out[0]=0x55;
	data_out[1]=0xaa;
	pkt->ver=0x00;
	data_out[2]=pkt->ver;
	data_out[3]=pkt->cmd;
	data_out[4]=pkt->len_num/256;
	pkt->len_h=pkt->len_num/256;
	data_out[5]=pkt->len_num%256;
	pkt->len_l=pkt->len_num%256;
	for(i=0;i<pkt->len_num;i++)
	{
		data_out[6+i]=pkt->data[i];
	}
	data_out[6+i]=check_sum(*pkt);
	put_ring(data_out,7+i,pkt->repeat_times);
}


void net_send_status(void)
{
	pkt_bbuf.ver=3;
	pkt_bbuf.cmd=ty_net;
	pkt_bbuf.len_num=0x01;
	pkt_bbuf.len_h=0x00;
	pkt_bbuf.len_l=0x01;
	pkt_bbuf.data=data_in_out;
	if(all_wifi==0x03)
	{
		data_in_out[0]=0;
	}
	else if(all_wifi==0x02)
	{

	}


}


//pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);




void notify_init(void)
{
	unsigned char i=0;
	notify_st.count=0;
	notify_st.step=0;
	notify_st.index_len=0;
	notify_st.had_change=0;
	for(i=0;i<NOTIFY_INDEX_MAX;i++)
	{
		notify_st.index_buf[i]=0xff;
		notify_st.index_buf_num[i]=0;
	}
	for(i=0;i<NOTIFY_CHAR_NUM_MAX;i++)
	{
		notify_st.char_index_raw[i]=0xff;
		notify_st.char_buf_len[i]=0;
//		notify_st.char_buf_add_sum[i]=0;
	}

	ZZC_LOG("notify_init");
}

unsigned char find_notify_index(unsigned char index,unsigned char *num)
{
	unsigned char ret=0;
	unsigned char i=0;
	for(i=0;i<notify_st.index_len;i++)
	{
		if(index==notify_st.index_buf[i])
		{
			ZZC_LOG("match index=%02x,i=%d,num=%d",index,i,notify_st.index_buf_num[i]);
			*num=notify_st.index_buf_num[i];
			return 1;
		}
	}
	return 0;
}

unsigned char find_notify_char_index(unsigned char index,unsigned char *num)
{
	unsigned char ret=0;
	unsigned char i=0;
	for(i=0;i<NOTIFY_CHAR_NUM_MAX;i++)
	{
		if(index==notify_st.char_index_raw[i])
		{
			ZZC_LOG("match char index=%02x,i=%d,num=%d",index,i,i);
			*num=i;
			return 1;
		}
	}
	return 0;
}


unsigned char notify_buf_old[NOTIFY_MAX]={0};

void copy_notify_buf_old(void)
{
	unsigned short i=0;
	unsigned char j=0;
	for(i=0;i<NOTIFY_MAX;i++)
	{
		notify_buf_old[i]=notify_st.buf[i];
	}
	for(j=0;j<NOTIFY_CHAR_NUM_MAX;j++)
	{
		if(notify_st.char_buf_len[j]>0)
		{
			ZZC_LOG("copy char %d",j);
			for(i=0;i<notify_st.char_buf_len[j];i++)
			{
				notify_buf_char_old[j][i]=notify_st.buf_char[j][i];
			}
		}
	}
}

void notify_add(unsigned char *buf,unsigned short in_len)
{
	unsigned short i=0,j=0;
	unsigned char index=0;
	unsigned char kind=0;
	unsigned short len=0;
	unsigned char index_num=0;
	unsigned char is_char=0;
	unsigned char char_num=0;
	unsigned char char_count=0;
	unsigned char char_had_change=0;
	unsigned char index_buf=0;
	time_get_deal_reset();
	if(get_query)
	{
		get_query=0;
		notify_init();
	}
	notify_had_add=1;
	for(i=0;i<in_len;)
	{
		index_buf=buf[i];
		if(index_buf==0x0e)
		{
			index=0xee;
		}
		else
		{
			index=index_buf;
		}

		kind=buf[i+1];
		is_char=0;
		if((kind==0x03||kind==0x00))
		{
			is_char=1;
			ZZC_LOG("is char or raw...........");
		}
		len=buf[i+2]*256+buf[i+3];
		if((in_len-i)<(4+len))
		{
			ZZC_LOG("point len is wrong len=%d",len);
			return ;
		}
		if(is_char==0)
		{
			index_num=0;
			if(find_notify_index(index,&index_num))
			{
				notify_st.buf[index_num]=index;index_num++;
				notify_st.buf[index_num]=ty_change_kind_up(kind,len)|((len/256)&0x0f);index_num++;
				notify_st.buf[index_num]=len;index_num++;
				for(j=0;j<len;j++)
				{
					if(is_in_every_times_setting(index))
					{
						notify_st.had_change|=2;
						ZZC_LOG("set_old notify_st.had_change =2,notify_buf_old[index_num]=%02x,buf[i+4+j]=%02x",notify_buf_old[index_num],buf[i+4+j]);
					}
					else if(notify_buf_old[index_num]!=buf[i+4+j])
					{
						if(is_in_must_setting(index))
						{
							notify_st.had_change|=2;
						}
						else
						{
							notify_st.had_change|=1;
						}
						ZZC_LOG("set_old notify_st.had_change =1,notify_buf_old[index_num]=%02x,buf[i+4+j]=%02x",notify_buf_old[index_num],buf[i+4+j]);
					}
					if(index_num<NOTIFY_MAX)
					{
						notify_st.buf[index_num]=buf[i+4+j];index_num++;
					}
				}

			}
			else
			{
				if(notify_st.index_len>=NOTIFY_INDEX_MAX)
				{
					return ;
				}
				notify_st.index_buf[notify_st.index_len]=index;
				notify_st.index_buf_num[notify_st.index_len]=notify_st.count;
				notify_st.index_len++;
				ZZC_LOG("notify_st add index=%02x,num=%d,index_len  after=%d",index,notify_st.count,notify_st.index_len);
				if(((in_len-i)<(4+len))||(notify_st.count+4+len>NOTIFY_MAX))
				{
					ZZC_LOG("notify_add len wrong,in_len=%d,3+len=%d",in_len,4+len);
					return ;
				}
				notify_st.buf[notify_st.count]=index;notify_st.count++;
				notify_st.buf[notify_st.count]=ty_change_kind_up(kind,len)|((len/256)&0x0f);notify_st.count++;
				notify_st.buf[notify_st.count]=len;notify_st.count++;
				for(j=0;j<len;j++)
				{
					notify_st.buf[notify_st.count]=buf[i+4+j];notify_st.count++;
				}
				if(is_in_must_setting(index)||is_in_every_times_setting(index))
				{
					notify_st.had_change|=2;
				}
				else
				{
					notify_st.had_change|=1;
				}
				ZZC_LOG("create new notify_st.had_change =1");


			}
			ZZC_LOG("notify_st.count=%d",notify_st.count);
		}
		else
		{
			if(4+len>NOTIFY_CHAR_MAX)
			{
				ZZC_LOG("notify_add char len wrong,char len=%d",len);
				return ;
			}
			if(find_notify_char_index(index,&char_num))
			{
				if(char_num>=NOTIFY_CHAR_NUM_MAX)
				{
					ZZC_LOG("char_num is bigger than NOTIFY_CHAR_NUM_MAX");
					return ;
				}
				char_count=0;
				notify_st.buf_char[char_num][char_count]=index;char_count++;
				notify_st.buf_char[char_num][char_count]=ty_change_kind_up(kind,len)|((len/256)&0x0f);char_count++;
				notify_st.buf_char[char_num][char_count]=len;char_count++;
//				char_add_sum=0;
				for(j=0;j<len;j++)
				{
//					char_add_sum+=buf[i+4+j];
					notify_st.buf_char[char_num][char_count]=buf[i+4+j];char_count++;
				}
				if(is_in_every_times_setting(index))
				{
					notify_st.had_change|=2;
					ZZC_LOG("set char notify_st.had_change =2 j=%d",j);
				}
				char_had_change=0;
				for(j=0;j<char_count;j++)
				{
					if(notify_buf_char_old[char_num][j]!=notify_st.buf_char[char_num][j])
					{
						char_had_change=1;
						break;
					}
				}
				if(char_had_change)
				{
					if(is_in_must_setting(index))
					{
						notify_st.had_change|=2;
					}
					else
					{
						// if(notify_st.had_change!=2)
						// {
							notify_st.had_change|=1;
						// }
					}
					ZZC_LOG("set char notify_st.had_change =1 j=%d",j);
				}
				notify_st.char_buf_len[char_num]=char_count;
			}
			else
			{
				if(find_notify_char_index(0xff,&char_num))
				{
					if(char_num>=NOTIFY_CHAR_NUM_MAX)
					{
						ZZC_LOG("2char_num is bigger than NOTIFY_CHAR_NUM_MAX");
						return ;
					}
					notify_st.char_index_raw[char_num]=index;
					char_count=0;
					notify_st.buf_char[char_num][char_count]=index;char_count++;
					notify_st.buf_char[char_num][char_count]=ty_change_kind_up(kind,len)|((len/256)&0x0f);char_count++;
					notify_st.buf_char[char_num][char_count]=len;char_count++;
//					char_add_sum=0;
					for(j=0;j<len;j++)
					{
//						char_add_sum+=buf[i+4+j];
						notify_st.buf_char[char_num][char_count]=buf[i+4+j];char_count++;
					}
//					notify_st.char_buf_add_sum[char_num]=char_add_sum;
					notify_st.char_buf_len[char_num]=char_count;
					if(is_in_must_setting(index)||is_in_every_times_setting(index))
					{
						notify_st.had_change|=2;
					}
					else
					{
						notify_st.had_change|=1;
					}
					ZZC_LOG("create new char notify_st.had_change =1,char num=%d,notify_st.char_buf_len[char_num]=%d",char_num,notify_st.char_buf_len[char_num]);
				}
				else
				{
					ZZC_LOG("notify char raw is full");
				}
			}
		}
		i=i+4+len;
	}
	ZZC_LOG("notify_st.buf:");
	print_x(notify_st.buf,notify_st.count);
	ZZC_LOG("notify_st.char_index_raw:");
	print_x(notify_st.char_index_raw,NOTIFY_CHAR_NUM_MAX);
}

extern void lj_updata_time(void);

void lj_usart_get_deal(lj_uchar* buf,lj_uint_16 len)
{
	lj_uint_16 i=0;
	lj_time_t time_buf;
	unsigned char mac[6]={0};
	static unsigned char old_buf=0;
#ifdef add_ff
	unsigned char fd_buf=0;
	unsigned int len_buf=0;
	unsigned short ver_len=0;
#endif
	unsigned short check=0;
	unsigned char ret=0;
	unsigned char j=0,k=0;
	unsigned char m=0;

	unsigned char status_change=0;
	static unsigned char check_change_85_ret=0;
	static unsigned char check_change_86_ret=0;
	static unsigned char check_change_86_all_ret=0;
	if(init_pass!=0xf5)
	{
		ZZC_LOG("init_pass_wrong\r\n");
		return ;
	}
//	time_get_deal_reset();
	for(i=0;i<len;i++)
	{

//			ZZC_LOG("step=%d",pkt_abuf.step);
			switch(pkt_abuf.step)
			{
				case 0:
#ifdef add_ff
				if(buf[i]==0xff)
				{
					lj_usart_pkt_init(&pkt_abuf);
					pkt_abuf.step=101;
				}
				else if(buf[i]==0x55)
				{
					lj_usart_pkt_init(&pkt_abuf);
					pkt_abuf.step++;
				}
#endif

				break;
				case 0x01:
					if(buf[i]==0xaa)
					{
						pkt_abuf.step++;
					}
					else
					{
						lj_usart_pkt_init(&pkt_abuf);
					}
					break;
				case 0x02:
					pkt_abuf.ver=buf[i];
					pkt_abuf.step++;
					break;
				case 0x03:
					pkt_abuf.cmd=buf[i];
					pkt_abuf.step++;
					break;
				case 0x04:
					pkt_abuf.len_h=buf[i];
					pkt_abuf.step++;
					break;
				case 0x05:
					pkt_abuf.len_l=buf[i];
					pkt_abuf.len_num=pkt_abuf.len_h*256+pkt_abuf.len_l;
					if(pkt_abuf.len_num==0)
					{
						pkt_abuf.step=7;
					}
					else
					{
						pkt_abuf.step++;
						pkt_abuf.count=0;
					}
					break;
				case 0x06:
					if(pkt_abuf.count>=data_in_max)
					{
						lj_usart_pkt_init(&pkt_abuf);
						break;
					}
					data_in[pkt_abuf.count]=buf[i];
					pkt_abuf.count++;
					if(pkt_abuf.count>=(pkt_abuf.len_num))
					{
						pkt_abuf.step++;
					}
					break;
				case 0x07:
					pkt_abuf.check_num=buf[i];
					pkt_abuf.data=data_in;
					if(check_sum(pkt_abuf)==pkt_abuf.check_num)
					{
						switch(pkt_abuf.cmd)
						{
						case ty_hb:
//							pkt_bbuf.ver=0x03;
//							pkt_bbuf.cmd = ty_hb;
//							pkt_bbuf.len_num = 1;
//							pkt_bbuf.data = data_in_out;
//							data_in_out[0]=1;
//							make_out(&pkt_bbuf);
							break;
						case ty_ask_net:
							lj_usend_wifi_net_status_ty_ask_net(all_wifi,all_net);
							break;
						case ty_reset:
							pkt_bbuf.ver=3;
							pkt_bbuf.cmd=ty_reset;
							pkt_bbuf.len_num=0;
							data_in_out[0]=0x01;
							pkt_bbuf.data=data_in_out;
							pkt_bbuf.repeat_times=0;
							make_out(&pkt_bbuf);
							pkt_st_abuf.delayms(100);
							pkt_st_abuf.reset_sdk();
							pkt_st_abuf.delayms(100);
							pkt_st_abuf.delayms(100);
							break;
						case ty_reset_mode:
							pkt_bbuf.ver=3;
							pkt_bbuf.cmd=ty_reset_mode;
							pkt_bbuf.len_num=0;
							data_in_out[0]=0x01;
							pkt_bbuf.data=data_in_out;
							pkt_bbuf.repeat_times=0;
							make_out(&pkt_bbuf);
							pkt_st_abuf.delayms(100);
							pkt_st_abuf.reset_sdk();
							pkt_st_abuf.delayms(100);
							pkt_st_abuf.delayms(100);
							break;
						case ty_get_time:
							time_buf;
							pkt_st_abuf.get_time(&time_buf);
							break;
						case ty_test_wifi:
							pkt_st_abuf.set_dis(1);
							break;
						case ty_notify:
							notify_add(pkt_abuf.data,pkt_abuf.len_num);
							break;


						}
					}
					else
					{
						ZZC_LOG("wrong check sum");
					}
					lj_usart_pkt_init(&pkt_abuf);
					break;


#ifdef add_ff
				case 101:
//					ZZC_LOG("ff_step=%d",pkt_abuf.ff_step);
					if(buf[i]==0xff)
					{
						lj_usart_pkt_init(&pkt_abuf);
						pkt_abuf.step=101;
					}
					else if(buf[i]==0xfe)
					{
						if(pkt_abuf.ff_step==7)
						{
							pkt_abuf.data=data_in;
							usart_get=1;
							if (usart_get)
							{
								usart_get = 0;
								if (check_xor(pkt_abuf) == pkt_abuf.check_num)
								{
									switch (pkt_abuf.cmd)
									{
									case c_get_mac:
									{
										if (pkt_st_abuf.get_mac(data_in_out))
										{
											//					u_printf("bbbbbbbbbbbbbbbbbbbbb_yes");
										} else {
											//					u_printf("bbbbbbbbbbbbbbbbbbbbb_no");
										}

										pkt_bbuf.cmd = c_get_mac;
										pkt_bbuf.len_num = 6;
										pkt_bbuf.id_dir = d_dir_in;
										pkt_bbuf.r_id = pkt_abuf.r_id;
										pkt_bbuf.data = data_in_out;
										pkt_bbuf.repeat_times=0;
										//	pkt_bbuf.check_num=check_xor(data_in,12);
										ff_make_out(&pkt_bbuf);
									}
									break;
									case c_get_connect:
									{
										pkt_st_abuf.ask_net(&data_in_out[0],&data_in_out[1]);
									//	lj_usart_pkt pkt_bbuf;
										pkt_bbuf.cmd=c_get_connect;
										pkt_bbuf.len_num=2;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
										pkt_bbuf.repeat_times=0;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										ff_make_out(&pkt_bbuf);
									}
									break;
									case c_get_idkey:
								//	else if(pkt_abuf.cmd==c_get_idkey)
									{
										pkt_st_abuf.get_idkey(data_in_out,&len_buf);
									//	lj_usart_pkt pkt_bbuf;
										pkt_bbuf.cmd=c_get_idkey;
										pkt_bbuf.len_num=len_buf;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										pkt_bbuf.repeat_times=0;
										ff_make_out(&pkt_bbuf);
									}
									break;
									case c_set_idkey:
								//	else if(pkt_abuf.cmd==c_set_idkey)
									{
										if(pkt_st_abuf.set_idkey((lj_uchar*)pkt_abuf.data,pkt_abuf.len_num-5))
										{
											data_in_out[0]=1;
										}
										else
										{
											data_in_out[0]=0;
										}
									//	lj_usart_pkt pkt_bbuf;
										pkt_bbuf.cmd=c_set_idkey;
										pkt_bbuf.len_num=1;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										ff_make_out(&pkt_bbuf);
									}
									break;
									case c_restart:
						//			else if(pkt_abuf.cmd==c_restart)
									{

						//				pkt_st_abuf.get_idkey(data_in);
									//	lj_usart_pkt pkt_bbuf;
										pkt_bbuf.cmd=c_restart;
										pkt_bbuf.len_num=1;
										data_in_out[0]=0x01;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
										pkt_bbuf.repeat_times=0;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										ff_make_out(&pkt_bbuf);
									//	pkt_st_abuf.delayms(100);
										pkt_st_abuf.restart();
									}
									break;
									case c_reset:
							//		else if(pkt_abuf.cmd==c_reset)
									{

						//				pkt_st_abuf.get_idkey(data_in);
									//	lj_usart_pkt pkt_bbuf;
										pkt_bbuf.cmd=c_reset;
										pkt_bbuf.len_num=1;
										data_in_out[0]=0x01;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
										pkt_bbuf.repeat_times=0;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										ff_make_out(&pkt_bbuf);
										pkt_st_abuf.delayms(100);
										pkt_st_abuf.reset_sdk();
										pkt_st_abuf.delayms(100);
									//	pkt_st_abuf.reload();
										pkt_st_abuf.delayms(100);
									//	pkt_st_abuf.restart();
									}
									break;
									case c_get_ver:
								//	else if(pkt_abuf.cmd==c_get_ver)
									{

										pkt_st_abuf.get_ver(data_in_out,&ver_len);
									//	lj_usart_pkt pkt_bbuf;
										pkt_bbuf.cmd=c_get_ver;
										pkt_bbuf.len_num=ver_len;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										pkt_bbuf.repeat_times=0;
										ff_make_out(&pkt_bbuf);
									}
									break;
									case c_get_time:
								//	else if(pkt_abuf.cmd==c_get_time)
									{
										lj_time_t time_buf;
										pkt_st_abuf.get_time(&time_buf);
								//		lj_usart_pkt pkt_bbuf;
									// 	pkt_bbuf.cmd=c_get_time;
									// 	pkt_bbuf.len_num=9;//8
									// 	data_in_out[0]=time_buf.year>>8;
									// 	data_in_out[1]=time_buf.year;
									// 	data_in_out[2]=time_buf.month;
									// 	data_in_out[3]=time_buf.day;
									// 	data_in_out[4]=time_buf.hour;
									// 	data_in_out[5]=time_buf.minute;
									// 	data_in_out[6]=time_buf.second;
									// 	data_in_out[7]=time_buf.area;
									// 	data_in_out[8]=time_buf.week;
									// 	pkt_bbuf.id_dir=d_dir_in;
									// 	pkt_bbuf.r_id=pkt_abuf.r_id;
									// 	pkt_bbuf.data=data_in_out;
									// //	pkt_bbuf.check_num=check_xor(data_in,2);
									// 	make_out(&pkt_bbuf);
									}
									break;
									case c_set_dis:
							//		else if(pkt_abuf.cmd==c_set_dis)
									{
										if(pkt_abuf.len_num==0x09)
										{
											if((pkt_abuf.data[0]==2)&&(pkt_abuf.data[1]==0)&&(pkt_abuf.data[2]==1)&&(pkt_abuf.data[3]==9))
											{
												pkt_st_abuf.set_dis(1);
												data_in_out[0]=0x01;
											}
											else
											{
												data_in_out[0]=0x00;
											//	pkt_st_abuf.set_dis(0);
											}
									//		lj_usart_pkt pkt_bbuf;
											pkt_bbuf.cmd=c_set_dis;
											pkt_bbuf.len_num=1;
			//								data_in_out[0]=0x01;
											pkt_bbuf.id_dir=d_dir_in;
											pkt_bbuf.r_id=pkt_abuf.r_id;
											pkt_bbuf.data=data_in_out;
										//	pkt_bbuf.check_num=check_xor(data_in,2);
											pkt_bbuf.repeat_times=0;
											ff_make_out(&pkt_bbuf);
										}
									}
									break;
								case c_rec_point_npush:
#ifdef _use_caipu_
									if(caipu_deal_85(pkt_abuf.data,(pkt_abuf.len_num)-5)==0)
									{

									}
									else
									{
										if((lj_push_mode==LJ_PUSH_MODE_ALL_CAN)||(lj_force_push))
										{
											data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
											if(lj_force_push)
											{
												set_caipu_change();
											}
											lj_force_push=0;
										}
										else
										{
											if(check_change(lj_setting,lj_setting_len)||(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI))
											{
												lj_time_can_push=0;
												data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
											}
											else
											{
												data_in_out[0]=0x01;
											}

										}
									}
									reset_change();
									if(caipu_is_running())
									{

									}
									else
									{
										pkt_bbuf.cmd=c_rec_point_npush;
										pkt_bbuf.len_num=1;
									//	data_in[0]=0x01;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
										pkt_bbuf.repeat_times=0;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										ff_make_out(&pkt_bbuf);
									}
#else //_use_caipu_
#if 0
									caipu_deal_85(pkt_abuf.data,(pkt_abuf.len_num)-5);
									if((lj_push_mode==LJ_PUSH_MODE_ALL_CAN)||(lj_force_push))
									{
										data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
										lj_force_push=0;
									}
									else
									{
										if(check_change(lj_setting,lj_setting_len)||(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI))
										{
											lj_time_can_push=0;
											data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
										}
										else
										{
											data_in_out[0]=0x01;
										}

									}


									reset_change();
#endif

									data_in_out[0]=0;
									check_change_85_ret|=check_change_85(pkt_abuf.data,(pkt_abuf.len_num)-5);
									ZZC_LOG("check_change_85_ret=%02x",check_change_85_ret);

									if((time_can_up&&(check_change_85_ret&HAD_CHANGE_SETTING))||(time_can_up&&(time_can_up_dingshi>time_can_up_dingshi_set)&&(check_change_85_ret&HAD_CHANGE_ALL))||(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI)||lj_force_push)
									{
										if(time_can_up_dingshi>time_can_up_dingshi_set)
										{
											ZZC_LOG("time_can_up_dingshi>time_can_up_dingshi_set\r\n");
										}
										if(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI)
										{
											ZZC_LOG("lj_time_can_push>LJ_TIME_CAN_PUSH_ORI\r\n");
										}
										if(lj_force_push)
										{
											ZZC_LOG("lj_force_push\r\n");
										}
										lj_time_can_push=0;
										lj_force_push=0;
										time_can_up_dingshi=0;
										check_change_85_ret=0;
										data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);

									}
									else
									{
										ZZC_LOG("the same.....\r\n");
									}

									pkt_bbuf.cmd=c_rec_point_npush;
									pkt_bbuf.len_num=1;
								//	data_in[0]=0x01;
									pkt_bbuf.id_dir=d_dir_in;
									pkt_bbuf.r_id=pkt_abuf.r_id;
									pkt_bbuf.data=data_in_out;
									pkt_bbuf.repeat_times=0;
								//	pkt_bbuf.check_num=check_xor(data_in,2);
									ff_make_out(&pkt_bbuf);
#endif //_use_caipu_
									break;
								case c_rec_hex_npush:
//									caipu_deal_86(pkt_abuf.data,(pkt_abuf.len_num)-5);
//									if((lj_push_mode==LJ_PUSH_MODE_ALL_CAN)||(lj_force_push))
//									{
//										data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
//										lj_force_push=0;
//									}
//									else
//									{
//										if(check_change(lj_setting,lj_setting_len)||(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI))
//										{
//											lj_time_can_push=0;
//											data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
//										}
//										else
//										{
//											data_in_out[0]=0x01;
//										}
//
//									}
//									reset_change();
#if 0
									if((check_change_86(pkt_abuf.data,(pkt_abuf.len_num)-5))||(time_can_up&&(check_change_86_all(pkt_abuf.data,(pkt_abuf.len_num)-5)))||(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI)||lj_force_push)
									{
										lj_time_can_push=0;
										lj_force_push=0;
										data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);
									}
#endif
									data_in_out[0]=0;

									check_change_86_ret|=check_change_86(pkt_abuf.data,(pkt_abuf.len_num)-5);
									if(setting_86_len==0)
									{
									check_change_86_all_ret|=check_change_86_all(pkt_abuf.data,(pkt_abuf.len_num)-5);
									}

									if((time_can_up&&check_change_86_ret)||(time_can_up&&(time_can_up_dingshi>time_can_up_dingshi_set)&&check_change_86_all_ret)||(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI)||lj_force_push)
									{
										if(time_can_up_dingshi>time_can_up_dingshi_set)
										{
											ZZC_LOG("time_can_up_dingshi>time_can_up_dingshi_set\r\n");
										}
										if(lj_time_can_push>LJ_TIME_CAN_PUSH_ORI)
										{
											ZZC_LOG("lj_time_can_push>LJ_TIME_CAN_PUSH_ORI\r\n");
										}
										if(lj_force_push)
										{
											ZZC_LOG("lj_force_push\r\n");
										}
										check_change_86_ret=0;
										check_change_86_all_ret=0;
										lj_time_can_push=0;
										lj_force_push=0;
										time_can_up_dingshi=0;
										data_in_out[0]=pkt_st_abuf.publish_point_npush_raw(pkt_abuf.data,(pkt_abuf.len_num)-5,pkt_abuf);

									}
									else
									{
										ZZC_LOG("the same.....\r\n");
									}

#ifdef _use_caipu_
									if(caipu_is_running())
									{

									}
									else
									{
										pkt_bbuf.cmd=c_rec_hex_npush;
										pkt_bbuf.len_num=1;
									//	data_in[0]=0x01;
										pkt_bbuf.id_dir=d_dir_in;
										pkt_bbuf.r_id=pkt_abuf.r_id;
										pkt_bbuf.data=data_in_out;
									//	pkt_bbuf.check_num=check_xor(data_in,2);
										pkt_bbuf.repeat_times=0;
										ff_make_out(&pkt_bbuf);
									}
#else //_use_caipu_

									pkt_bbuf.cmd=c_rec_hex_npush;
									pkt_bbuf.len_num=1;
								//	data_in[0]=0x01;
									pkt_bbuf.id_dir=d_dir_in;
									pkt_bbuf.r_id=pkt_abuf.r_id;
									pkt_bbuf.data=data_in_out;
								//	pkt_bbuf.check_num=check_xor(data_in,2);
									pkt_bbuf.repeat_times=0;
									ff_make_out(&pkt_bbuf);
#endif //_use_caipu_
									break;


									}
								} else {
//													LOG("XOR WRONG!!!");
								}
							}
						}
						lj_usart_pkt_init(&pkt_abuf);
					}
					else
					{
						if(buf[i]==0xfd)
						{
							lj_get_fd=1;
							break;
//							continue;
						}
						if(lj_get_fd==1)
						{
							lj_get_fd=0;
							if(buf[i]==0x0f)
							{
								fd_buf=0xff;
							}
							else if(buf[i]==0x0e)
							{
								fd_buf=0xfe;
							}
							else if(buf[i]==0x0d)
							{
								fd_buf=0xfd;
							}
						//	pkt_abuf.data[pkt_abuf.len_num]=fd_buf;
						}
						else
						{
							fd_buf=buf[i];
							//pkt_abuf.data[pkt_abuf.len_num]=buf[i];
						}
						if(pkt_abuf.ff_step==1)
						{
							pkt_abuf.ff_step=2;
							pkt_abuf.len_h=fd_buf;
						//	pkt_abuf.check_num_buf^=fd_buf;
						}
						else if(pkt_abuf.ff_step==2)
						{
							pkt_abuf.ff_step=3;
							pkt_abuf.len_l=fd_buf;
							pkt_abuf.len_num=((pkt_abuf.len_h)<<8)+pkt_abuf.len_l;
							if(pkt_abuf.len_num>data_in_max-5)
							{
								lj_usart_pkt_init(&pkt_abuf);
							}
						//	pkt_abuf.check_num_buf^=fd_buf;
						}
						else if(pkt_abuf.ff_step==3)
						{
							pkt_abuf.ff_step=4;
							pkt_abuf.cmd=fd_buf;
						//	pkt_abuf.check_num_buf^=fd_buf;
						}
						else if(pkt_abuf.ff_step==4)
						{
							pkt_abuf.ff_step=5;
							pkt_abuf.r_id=fd_buf;
							u_rec_id=pkt_abuf.r_id;
							ZZC_LOG("u_send_id=%02x,u_rec_id=%02x",u_send_id,u_rec_id);
							if(u_send_id==u_rec_id)
							{
								get_feedback=1;
							}
							if(pkt_abuf.len_num<=0x05)
							{
								pkt_abuf.ff_step=6;
							}
						//	pkt_abuf.check_num_buf^=fd_buf;
						}
						else if(pkt_abuf.ff_step==5)
						{
							data_in[pkt_abuf.count]=fd_buf;
							pkt_abuf.count++;
							if(pkt_abuf.count>=(pkt_abuf.len_num)-5)
							{
								pkt_abuf.ff_step=6;
							}
						//	pkt_abuf.check_num_buf^=fd_buf;
						}
						else if(pkt_abuf.ff_step==6)
						{
							pkt_abuf.check_num=fd_buf;
							pkt_abuf.ff_step=7;
						}
					}
					break;
#endif


			}//switch
	}//for
}

#ifdef _use_caipu_

extern void lj_set_zhufan_total_time(void);

void air_send_zhufan_time(unsigned int time)
{
	unsigned char buf[7+7]={0};

	zhufan_time_min_total=zhufan_time_min_total+time;
	lj_set_zhufan_total_time();

	buf[0]=0x11;
	buf[1]=0x40;
	buf[2]=0x04;
	buf[3]=time/256/256/256;
	buf[4]=((time/256/256)&0xff);
	buf[5]=((time/256)&0xff);
	buf[6]=time%256;
	buf[7]=0x12;
	buf[8]=0x40;
	buf[9]=0x04;
	buf[10]=zhufan_time_min_total/256/256/256;
	buf[11]=((zhufan_time_min_total/256/256)&0xff);
	buf[12]=((zhufan_time_min_total/256)&0xff);
	buf[13]=zhufan_time_min_total%256;
	pkt_st_abuf.publish_point_npush_raw_85(buf,14);
	ZZC_LOG("air_send_zhufan_time[]=%02x,%02x,%02x,%02x\r\n",buf[3],buf[4],buf[5],buf[6]);
	ZZC_LOG("total[]=%02x,%02x,%02x,%02x\r\n",buf[10],buf[11],buf[12],buf[13]);
}



void air_send_zhufan_time_loop(void)//100ms
{
	unsigned char index_0=0;
	unsigned char index_1=0;
	unsigned int value_0=0;
	unsigned int value_1=0;
	static unsigned char iszhufaning=0;
	static unsigned int zhufan_time_div=0;
	static unsigned int zhufan_time_second=0;
	static unsigned int zhufan_time_min=0;

	zhufan_time_div++;
	if(zhufan_time_div>=10)//10
	{
		zhufan_time_div=0;
		index_0=point_find_int_num(0x00);
		index_1=point_find_int_num(0x01);

		if((index_0!=0xff)&&(index_1!=0xff))
		{
			get_point_int(index_0,&value_0);
			get_point_int(index_1,&value_1);
			if((value_0==1)&&(value_1==0x01))
			{
				if(iszhufaning==0)
				{
					iszhufaning=1;
					zhufan_time_second=0;
					zhufan_time_min=0;
				}
			}
			else
			{
				if(iszhufaning==1)
				{
					iszhufaning=0;
					if(zhufan_time_min!=0)
					{
						air_send_zhufan_time(zhufan_time_min);
					}
					zhufan_time_second=0;
					zhufan_time_min=0;
				}
			}
		}
		if(iszhufaning)
		{
			zhufan_time_second++;
			if(zhufan_time_second>=60)//60
			{
				zhufan_time_second=0;
				zhufan_time_min++;
				ZZC_LOG("zhufan_time_min=%d\r\n",zhufan_time_min);
			}
		}

	}


}
#endif //_use_caipu_

#define SETTING_86_MAX (64)
unsigned char setting_86_buf[SETTING_86_MAX]={0};//just the count of 86 raw,but not the real 85 index
//unsigned short setting_86_len=0;
unsigned char setting_86_status_old[SETTING_86_MAX]={0};


unsigned char set_86_setting(unsigned char *buf ,unsigned short len)
{
	unsigned short i=0;
	unsigned char change_buf =0;
	setting_86_len=len;
	if (len>SETTING_86_MAX)
	{
		len=SETTING_86_MAX;
		setting_86_len=SETTING_86_MAX;
	}
	for(i=0;i<len;i++)
	{
		if(setting_86_buf[i]!=buf[i])
		{
			change_buf=1;
			setting_86_buf[i]=buf[i];
		}
	}
	if(change_buf)
	{
		for(i=0;i<len;i++)
		{
			setting_86_status_old[i]=0xff;
		}

		ZZC_LOG("set_86_setting......setting_86_len=%d\r\n",setting_86_len);
	}
	return 1;
}

unsigned char  check_change_86(unsigned char *buf,unsigned short len)
{
	unsigned short i=0;
	unsigned char ret=0;
	if(setting_86_len==0)
	{

		ZZC_LOG("setting_86_len=0");
		return check_change_86_all(buf,len);
	}
	if(len==0)
	{
		ZZC_LOG("check_change_86 in len=0");
		ret=0;
		return ret;
	}
	for(i=0;i<setting_86_len;i++)
	{
		if(setting_86_buf[i]>=len)
		{
			ZZC_LOG("setting_86_buf[%d] is bigger than len =%d\r\n",i,len);
			continue;
		}
		else
		{
			if(setting_86_status_old[i]!=buf[setting_86_buf[i]])
			{

				ZZC_LOG("setting_86_status_old[%d]=%02x,but buf[setting_86_buf[i]=%02x\r\n",i,setting_86_status_old[i],buf[setting_86_buf[i]]);
				setting_86_status_old[i]=buf[setting_86_buf[i]];
				ret=1;
			}
		}
	}
	return ret;
}


#define STATUS_86_MAX (128)
unsigned char status_86_old[STATUS_86_MAX]={0};
unsigned char check_change_86_all(unsigned char *buf,unsigned short len)
{
	unsigned short i=0;
	unsigned char ret=0;
	if(len>STATUS_86_MAX)
	{
		len=STATUS_86_MAX;
	}
	for(i=0;i<len;i++)
	{
		if(status_86_old[i]!=buf[i])
		{
			status_86_old[i]=buf[i];
			ret=1;
		}
	}
	if(ret)
	{
		ZZC_LOG("check_change_86_all change~~~~");
	}
	return ret;

}


#define POINT_85_MAX (64)
#define SETTING_85_MAX (64)
#define SETTING_85_LEN_MAX (12)
unsigned char setting_85_buf[SETTING_85_MAX]={0};
unsigned short setting_85_len=0;
unsigned char setting_85_status_old[POINT_85_MAX][SETTING_85_LEN_MAX]={0};


typedef struct{
	unsigned char is_setting;
	unsigned char had_change;
	unsigned char full;
	unsigned char index;
	unsigned char kind;
	unsigned char len;
	unsigned char status_buf[SETTING_85_LEN_MAX];
}point_85_st;

point_85_st point_85[POINT_85_MAX];


void print_point_85(void)
{
    unsigned char i=0,j=0;
	// log_zzc("");
    ZZC_LOG("full	index	kind	len 	had_change	 data \r\n");
    for(i=0;i<POINT_85_MAX;i++)
    {
    	if(point_85[i].full)
    	{
			ZZC_LOG("%d    %02x    %02x    %02x    %02x    ",point_85[i].full,point_85[i].index,point_85[i].kind,point_85[i].len,point_85[i].had_change);
			for(j=0;j<point_85[i].len;j++)
			{
				ZZC_LOG(" %02x",*((unsigned char *)(&point_85[i].status_buf)+j));
			}
//			ZZC_LOG("\r\n");
    	}
    }
}

unsigned char set_85_setting(unsigned char *buf ,unsigned short len)
{
	unsigned short i=0;
	unsigned char change_buf =0;
	setting_85_len=len;
	if (len>SETTING_85_MAX)
	{
		len=SETTING_85_MAX;
		setting_85_len=SETTING_85_MAX;
	}
	for(i=0;i<len;i++)
	{
		if(setting_85_buf[i]!=buf[i])
		{
			change_buf=1;
			setting_85_buf[i]=buf[i];
		}
	}
	if(change_buf)
	{
		setting_85_status_old[0][0]=0xff;
		ZZC_LOG("set_85_setting......setting_85_len=%d\r\n",setting_85_len);
	}
	return 1;
}

#define SETTING_MUST_LEN_MAX (5)
unsigned char setting_must_buf[SETTING_MUST_LEN_MAX]={0};
unsigned char setting_must_len=0;

unsigned char set_must_setting(unsigned char *buf ,unsigned short len)
{
	unsigned short i=0;
	unsigned char change_buf =0;
	setting_must_len=len;
	if (len>SETTING_MUST_LEN_MAX)
	{
		len=SETTING_MUST_LEN_MAX;
		setting_must_len=SETTING_MUST_LEN_MAX;
	}
	for(i=0;i<setting_must_len;i++)
	{
		if(setting_must_buf[i]!=buf[i])
		{
			change_buf=1;
			setting_must_buf[i]=buf[i];
		}
	}
	if(change_buf)
	{

//		setting_85_status_old[0][0]=0xff;
		ZZC_LOG("set_must_setting......setting_must_len=%d\r\n",setting_must_len);
		print_x(setting_must_buf,setting_must_len);
	}
	return 1;
}

unsigned char is_in_must_setting(unsigned char index)
{
	unsigned char i=0;
	if(setting_must_len==0)
	{
		return 0;
	}
	for(i=0;i<setting_must_len;i++)
	{
		if(setting_must_buf[i]==index)
		{
			ZZC_LOG("index %02x is_in_must_setting i=%d",index,i);
			return 1;
		}
	}
//	ZZC_LOG("not in must setting");
	return 0;
}


#define SETTING_EVERY_TIMES_LEN_MAX (5)
unsigned char setting_every_times_buf[SETTING_EVERY_TIMES_LEN_MAX]={0};
unsigned char setting_every_times_len=0;

unsigned char set_every_times_setting(unsigned char *buf ,unsigned short len)
{
	unsigned short i=0;
	unsigned char change_buf =0;
	setting_every_times_len=len;
	if (len>SETTING_EVERY_TIMES_LEN_MAX)
	{
		len=SETTING_EVERY_TIMES_LEN_MAX;
		setting_every_times_len=SETTING_EVERY_TIMES_LEN_MAX;
	}
	for(i=0;i<setting_every_times_len;i++)
	{
		if(setting_every_times_buf[i]!=buf[i])
		{
			change_buf=1;
			setting_every_times_buf[i]=buf[i];
		}
	}
	if(change_buf)
	{

//		setting_85_status_old[0][0]=0xff;
		ZZC_LOG("set_every_times_setting......setting_every_times_len=%d\r\n",setting_every_times_len);
		print_x(setting_every_times_buf,setting_every_times_len);
	}
	return 1;
}

unsigned char is_in_every_times_setting(unsigned char index)
{
	unsigned char i=0;
	if(setting_every_times_len==0)
	{
		return 0;
	}
	for(i=0;i<setting_every_times_len;i++)
	{
		if(setting_every_times_buf[i]==index)
		{
			ZZC_LOG("index %02x is_in_every_times_setting i=%d",index,i);
			return 1;
		}
	}
//	ZZC_LOG("not in must setting");
	return 0;
}


unsigned char set_all_setting(unsigned char *buf,unsigned short len)
{
	unsigned short i=0;
	unsigned short div[4]={0};
	unsigned char ret=0;
	unsigned char get_div=0;
	unsigned char j=0;
	unsigned short div_buf=0;
	if(len==0)
	{
		return 0;
	}
	for(i=0;i<len;i++)
	{
		if(buf[i]==0xef)
		{
			get_div++;
			if(j<4)
			{
				div[j]=i;
				j++;
			}
			else
			{
				return 0;
			}
//			break;
		}
	}
	if(get_div==0)
	{

//		ret=set_86_setting(buf,len);
//		ZZC_LOG("had 86 setting");
	}
	if(get_div>=1)
	{
		ret=set_86_setting(buf,div[0]);
		ZZC_LOG("had 86 setting");
		if(div[1]>div[0])
		{
//		if(len-1-div>0)
			ret=set_85_setting(&buf[div[0]+1],div[1]-1-div[0]);
			ZZC_LOG("had 85 setting");
		}
	}
	if(get_div>=2)
	{
		if(get_div==2)
		{
			div_buf=len;
		}
		else
		{
			div_buf=div[2];
		}
		if(div_buf>div[1])
		{
			set_must_setting(&buf[div[1]+1],div_buf-1-div[1]);
			ZZC_LOG("had must setting");
		}
	}
	if(get_div>=3)
	{
		if(get_div==3)
		{
			div_buf=len;
		}
		else
		{
			div_buf=div[3];
		}
		if(div_buf>div[2])
		{
			set_every_times_setting(&buf[div[2]+1],div_buf-1-div[2]);
			ZZC_LOG("had every_times setting");
		}
	}

	return 1;
}


unsigned char find_index_85(unsigned char index,unsigned char *out)
{
//	unsigned char ret=0;
	unsigned char i=0;
	for(i=0;i<POINT_85_MAX;i++)
	{
		if(point_85[i].full==1)
		{
			if(point_85[i].index==index)
			{
				*out=i;
				return 1;
			}
		}
	}
	return 0;
}



unsigned char is_in_setting_menu(unsigned char index)//if not set that all is in,if set just what set is in
{
	unsigned char i=0;
	unsigned char len=0;
	unsigned char ret=0;
	if(setting_85_len>POINT_85_MAX)
	{
		len=POINT_85_MAX;
	}
	else
	{
		len=setting_85_len;
	}
	if(setting_85_len==0)
	{
		ret =1;
	}
	else
	{
		for(i=0;i<len;i++)
		{
			if(index==setting_85_buf[i])
			{
				ret=1;
				break;
			}
		}
	}
	return ret;
}




//#define HAD_CHANGE_ALL (0X01)
//#define HAD_CHANGE_SETTING (0x10)

unsigned char  set_point_value_85(unsigned char index,unsigned char kind,unsigned short len,unsigned char *buf)
{
    unsigned  char i=0,j=0;
    unsigned short len_buf=0;
    unsigned char ret=0;
	// unsigned char had_change=0;
    if(len>SETTING_85_LEN_MAX)
	{
		len_buf=SETTING_85_LEN_MAX;
	}
    else
	{
    	len_buf=len;
	}
    for(i=0;i<POINT_85_MAX;i++)
    {
    	if(point_85[i].full)
    	{
    		if(point_85[i].index==index)
    		{
    			point_85[i].kind=kind;
    			point_85[i].len=len_buf;
    			for(j=0;j<len_buf;j++)
    			{
					if(point_85[i].status_buf[j]!=buf[j])
					{
						ZZC_LOG("point_85[%d].status_buf[%d]=%02x,but now is %02x\r\n",i,j,point_85[i].status_buf[j],buf[j]);
						point_85[i].status_buf[j]=buf[j];
						point_85[i].had_change=1;
						if(is_in_setting_menu(index))
						{
							ret|=HAD_CHANGE_SETTING;
						}
						ret|=HAD_CHANGE_ALL;
					}
    			}
    			break;

    		}
    	}
    	else
    	{
    		ZZC_LOG("a new point \r\n");
    		point_85[i].had_change=1;
    		point_85[i].index=index;
    		point_85[i].kind=kind;
    		point_85[i].len=len_buf;
    		point_85[i].full=1;
			for(j=0;j<point_85[i].len;j++)
			{
				point_85[i].status_buf[j]=buf[j];
			}
			if(is_in_setting_menu(index))
			{
				ret|=HAD_CHANGE_SETTING;
			}
			ret|=HAD_CHANGE_ALL;
			// had_change=1;
    		break;
    	}
    }
	if(i>=POINT_85_MAX)
	{
		ZZC_LOG("point char is full\r\n");
	}
	return ret;
	// return had_change;
}



unsigned char  check_change_85(unsigned char *buf,unsigned short len)
{
	unsigned short i=0,j=0;
	unsigned char ret=0;
	unsigned char ret_buf=0;
	unsigned char p_index=0;
	unsigned char p_len=0;
	unsigned char p_kind=0;
	unsigned char count=0;
	if(setting_85_len==0)
	{
//		ret|=HAD_CHANGE_SETTING;
		ZZC_LOG("setting_85_len=0");
//		return ret;
	}
	if(len==0)
	{
		ZZC_LOG("check_change_85 in len=0");
		ret=0;
		return ret;
	}
	for(i=0;i<len;)
	{
		p_index=buf[i];
		p_kind=buf[i+1];
		p_len=buf[i+2];
#if 0
		if(set_point_value_85(p_index,p_kind,p_len,&buf[i+3]))
		{
			ret =1;
		}
#endif
		ret_buf=set_point_value_85(p_index,p_kind,p_len,&buf[i+3]);
		{
			if(ret_buf)
			{
				ret|=ret_buf;
			}
		}
		i=i+3+p_len;
	}
	if(ret)
	{
		ZZC_LOG("check_change_85 ret=%02x________\r\n",ret);
		print_point_85();
	}
	return ret;
}


